import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import Login from './views/Login.vue';
import store from './store';
Vue.use(Router)
const vueRouter=new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
      meta:{auto:true}
    },
    {
      path:'/login',
      name:'login',
      component:Login
    }
  ]
})
vueRouter.beforeEach((to,from,next)=>{
    if(to.meta.auto){//需不需要登录验证
          if(store.state.token){
            //如果有token,已登录
            next();
          }else{
            //没有token,说明没有登录,让他去登录页
            vueRouter.push({
              path:'/login',
              query:{redirect:to.path}
            })
          }
    }else{
      next()
    }
})
export default vueRouter
